package com.example.fw;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends HelperBase {
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;
	

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	private SortedListOf<ContactData> cashedContacts;
	
	public SortedListOf<ContactData> getContacts() {
		if (cashedContacts == null){
			rebuildCashe();
		}
		return cashedContacts;
	}
	private void rebuildCashe() {
			cashedContacts = new SortedListOf<ContactData>();	
			manager.navigateTo().mainPage();
			List<WebElement> tableRecords = driver.findElements(By.xpath("//tr"));
			for (int i = 1; i < tableRecords.size() - 1; i++) {
				String factLastName = tableRecords.get(i).findElement(By.xpath(".//td[2]")).getText();
				String factFirstName = tableRecords.get(i).findElement(By.xpath(".//td[3]")).getText();
				ContactData contact = new ContactData()
				.withFirstName(factFirstName)
				.withLastName(factLastName);
				cashedContacts.add(contact);		
				}
	}

		public ContactHelper createContact(ContactData contact) {
			openNewContactCreationPage();
			fillContactData(contact);
			submitContactCreation();
			returnToContactsPage();
			rebuildCashe();
			return this;
		
		}
		public ContactHelper modifyContactWay1(int index, ContactData contact) {
			manager.navigateTo().mainPage();
			initContactModification(index);
			fillContactData(contact);
			submitContactModification();
			returnToContactsPage();
			rebuildCashe();
			return this;
		}

		public ContactHelper modifyContactWay2(int index, ContactData contact) {
			manager.navigateTo().mainPage();
			checkContactDetails(index);
			initContactDetailsModification();
			fillContactData(contact);
			submitContactModification();
			returnToContactsPage();
			rebuildCashe();
			return this;
		}

		public ContactHelper deleteContact(int index) {
			manager.navigateTo().mainPage();
			initContactModification(index);
			submitContactDeletion();
			returnToContactsPage();
			rebuildCashe();
			return this;
		}
		
//------------------------------------------------------------------------------------------------
		
	public ContactHelper fillContactData(ContactData contact) {
		
		type(By.name("firstname"), contact.getFirstName());
		type(By.name("lastname"), contact.getLastName());
		type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getHomePhoneNumber());
		type(By.name("mobile"), contact.getMobilePhoneNumber());
		type(By.name("work"), contact.getWorkPhoneNumber());
		type(By.name("email"), contact.getFirstEmail());
		type(By.name("email2"), contact.getSecondEmail());
		selectByText(By.name("bday"), contact.getBday());
		selectByText(By.name("bmonth"), contact.getBmonth());
		type(By.name("byear"), contact.getBirthYear());
		type(By.name("address2"), contact.getAddress2());
		type(By.name("phone2"), contact.getHomePhone2());
		selectByText(By.name("new_group"), contact.newGroup);
		return this;
	}

	
	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		return this;
	}

	public ContactHelper openNewContactCreationPage() {
		if (!onNewContactCreationPage()) {
			click(By.linkText("add new"));	
		}
		return this;
	}

	private boolean onNewContactCreationPage() {
		return (driver.findElements(By.name("new_group")).size() > 0);
	}

	public ContactHelper returnToContactsPage() {
		click(By.linkText("home page"));
		return this;
	}

	public ContactHelper initContactModification(int index) {
		click(By.xpath("//*[@id='maintable']/tbody/tr[" + (index+2) + "]/td[7]/a/img"));
		return this;
	}

	public void selectContactByIndex(int index) {
		click(By.xpath("//tr[" + (index+2) + "]/td/input"));
	}

	public ContactHelper submitContactDeletion() {
		click(By.xpath("//*[@id='content']/form[2]/input[2]"));
		return this;
	}

	public ContactHelper submitContactModification() {
		click(By.xpath("//input[@name='update'][@value='Update']"));
		return this;
	}

	public ContactHelper checkContactDetails(int index) {
		selectContactByIndex(index);
		click(By.xpath("//*[@id='maintable']/tbody/tr[" + (index+2) + "]/td[6]/a/img"));	
		return this;
	}

	public ContactHelper initContactDetailsModification() {
	click(By.name("modifiy"));	
	return this;
	}


}
